package com.xgp.labblog.service.Impl;

import com.xgp.labblog.mapper.SpeakMapper;
import com.xgp.labblog.mapper.SpeakMapperExt;
import com.xgp.labblog.model.Speak;
import com.xgp.labblog.model.SpeakExample;
import com.xgp.labblog.service.SpeakService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpeakServiceImpl implements SpeakService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SpeakMapper speakMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SpeakMapperExt speakMapperExt;

    @Override
    public List<Speak> findSpeakByWid(Integer id) {
        SpeakExample speakExample = new SpeakExample();
        speakExample.createCriteria().andWidEqualTo(id);
        speakExample.setOrderByClause("time desc");
        List<Speak> speaks = speakMapper.selectByExample(speakExample);
        return speaks;
    }

    @Override
    public List<Speak> findSpeakByWidAndTitle(Integer id, String title) {
        SpeakExample speakExample = new SpeakExample();
        speakExample.createCriteria().andWidEqualTo(id).andTextLike("%" + title + "%");
        speakExample.setOrderByClause("time desc");
        List<Speak> speaks = speakMapper.selectByExample(speakExample);
        return speaks;
    }

    @Override
    public void addSpeakById(Integer id, String text) {
        Speak speak = new Speak();
        speak.setWid(id);
        speak.setText(text);
        speak.setTime(System.currentTimeMillis());
        speakMapper.insertSelective(speak);
    }

    @Override
    public void addSupCount(Integer id) {
        speakMapperExt.addSupCountById(id);
    }
}
